import filecmp 
import glob
import shutil
import os
import sys
import subprocess
import logging
import utilsunit
import bots.botslib as botslib
import bots.botsinit as botsinit
import bots.botsglobal as botsglobal
from bots.botsconfig import *

'''plugin unitretry.zip'''
#20120917: retry command is no more ;-))
#activate routes
#not an acceptance test
''' input: mime (complex structure); 2 different edi attachments, and ' tekst' attachemnt
    some user scripts are written in this unit test; so one runs errors will occur; write user script which prevents error in next run
    before running: delete all transactions.
    runs OK if no errors in unit tests; that is : no exceptions are raised. The bots-engine runs do give errors, but this is needed for retries 
'''
    

def change_communication_type(idchannel,to_type):
    botslib.changeq(u'''UPDATE channel 
                        SET type = %(to_type)s
                        WHERE idchannel = %(idchannel)s
                        ''',{'to_type':to_type,'idchannel':idchannel})

def scriptwrite(path,content):
    f = open(path,'w')
    f.write(content)
    f.close()

def indicate_rereceive():
    count = 0
    for row in botslib.query(u'''SELECT idta
                            FROM    filereport
                            ORDER BY idta DESC
                            '''):
        count += 1
        botslib.changeq(u'''UPDATE filereport
                            SET retransmit = 1
                            WHERE idta=%(idta)s
                            ''',{'idta':row['idta']})
        if count >= 2:
            break

def indicate_send():
    count = 0
    for row in botslib.query(u'''SELECT idta
                            FROM    ta
                            WHERE status=%(status)s
                            ORDER BY idta DESC
                            ''',{'status':EXTERNOUT}):
        count += 1
        botslib.changeq(u'''UPDATE ta
                            SET retransmit = %(retransmit)s
                            WHERE idta=%(idta)s
                            ''',{'retransmit':True,'idta':row['idta']})
        if count >= 2:
            break


if __name__ == '__main__':
    pythoninterpreter = 'python2.7'
    botsinit.generalinit('config')
    utilsunit.dummylogger()
    botsinit.connect()
    
    #############route unitretry_automatic###################
    #channel has type file: this goes OK
    change_communication_type('unitretry_automatic_out','file')
    subprocess.call([pythoninterpreter,'bots-engine.py','unitretry_automatic','unitretry_automatic3'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':0,'lastreceived':2,'lasterror':0,'lastdone':2,'lastok':0,'lastopen':0,'send':2,'processerrors':0},utilsunit.getreportlastrun()) #check report
    #change channel type to ftp: errors (run twice)
    change_communication_type('unitretry_automatic_out','ftp')
    subprocess.call([pythoninterpreter,'bots-engine.py','unitretry_automatic','unitretry_automatic3'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':1,'lastreceived':2,'lasterror':2,'lastdone':0,'lastok':0,'lastopen':0,'send':0,'processerrors':1},utilsunit.getreportlastrun()) #check report
    subprocess.call([pythoninterpreter,'bots-engine.py','unitretry_automatic','unitretry_automatic3'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':1,'lastreceived':2,'lasterror':2,'lastdone':0,'lastok':0,'lastopen':0,'send':0,'processerrors':1},utilsunit.getreportlastrun()) #check report
    #change channel type to file and do automaticretrycommunication: OK
    change_communication_type('unitretry_automatic_out','file')
    subprocess.call([pythoninterpreter,'bots-engine.py','--automaticretrycommunication'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':0,'lastreceived':4,'lasterror':0,'lastdone':4,'lastok':0,'lastopen':0,'send':4,'processerrors':0},utilsunit.getreportlastrun()) #check report
    #run automaticretrycommunication again: no new run is made, same results as last run 
    subprocess.call([pythoninterpreter,'bots-engine.py','--automaticretrycommunication'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':0,'lastreceived':4,'lasterror':0,'lastdone':4,'lastok':0,'lastopen':0,'send':4,'processerrors':0},utilsunit.getreportlastrun()) #check report
    #channel has type file: this goes OK
    change_communication_type('unitretry_automatic_out','file')
    subprocess.call([pythoninterpreter,'bots-engine.py','unitretry_automatic','unitretry_automatic3'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':0,'lastreceived':2,'lasterror':0,'lastdone':2,'lastok':0,'lastopen':0,'send':2,'processerrors':0},utilsunit.getreportlastrun()) #check report
    #rereceive last 2 files
    indicate_rereceive()
    subprocess.call([pythoninterpreter,'bots-engine.py','--rereceive'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':0,'lastreceived':2,'lasterror':0,'lastdone':2,'lastok':0,'lastopen':0,'send':1,'processerrors':0},utilsunit.getreportlastrun()) #check report
    #resend last 2 files
    indicate_send()
    subprocess.call([pythoninterpreter,'bots-engine.py','--resend'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':0,'lastreceived':2,'lasterror':0,'lastdone':2,'lastok':0,'lastopen':0,'send':2,'processerrors':0},utilsunit.getreportlastrun()) #check report
    
    #***run with communciation errors, run OK, communciation errors, run OK, run automaticretry
    #change channel type to ftp: errors
    change_communication_type('unitretry_automatic_out','ftp')
    subprocess.call([pythoninterpreter,'bots-engine.py','unitretry_automatic','unitretry_automatic3'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':1,'lastreceived':2,'lasterror':2,'lastdone':0,'lastok':0,'lastopen':0,'send':0,'processerrors':1},utilsunit.getreportlastrun()) #check report
    #channel has type file: this goes OK
    change_communication_type('unitretry_automatic_out','file')
    subprocess.call([pythoninterpreter,'bots-engine.py','unitretry_automatic','unitretry_automatic3'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':0,'lastreceived':2,'lasterror':0,'lastdone':2,'lastok':0,'lastopen':0,'send':2,'processerrors':0},utilsunit.getreportlastrun()) #check report
    #change channel type to ftp: errors
    change_communication_type('unitretry_automatic_out','ftp')
    subprocess.call([pythoninterpreter,'bots-engine.py','unitretry_automatic','unitretry_automatic3'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':1,'lastreceived':2,'lasterror':2,'lastdone':0,'lastok':0,'lastopen':0,'send':0,'processerrors':1},utilsunit.getreportlastrun()) #check report
    #channel has type file: this goes OK
    change_communication_type('unitretry_automatic_out','file')
    subprocess.call([pythoninterpreter,'bots-engine.py','unitretry_automatic','unitretry_automatic3'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':0,'lastreceived':2,'lasterror':0,'lastdone':2,'lastok':0,'lastopen':0,'send':2,'processerrors':0},utilsunit.getreportlastrun()) #check report
    #change channel type to file and do automaticretrycommunication: OK
    change_communication_type('unitretry_automatic_out','file')
    subprocess.call([pythoninterpreter,'bots-engine.py','--automaticretrycommunication'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':0,'lastreceived':4,'lasterror':0,'lastdone':4,'lastok':0,'lastopen':0,'send':4,'processerrors':0},utilsunit.getreportlastrun()) #check report
    #change channel type to ftp: errors
    change_communication_type('unitretry_automatic_out','ftp')
    subprocess.call([pythoninterpreter,'bots-engine.py','unitretry_automatic','unitretry_automatic3'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':1,'lastreceived':2,'lasterror':2,'lastdone':0,'lastok':0,'lastopen':0,'send':0,'processerrors':1},utilsunit.getreportlastrun()) #check report
    #channel has type file: this goes OK
    change_communication_type('unitretry_automatic_out','file')
    subprocess.call([pythoninterpreter,'bots-engine.py','unitretry_automatic','unitretry_automatic3'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':0,'lastreceived':2,'lasterror':0,'lastdone':2,'lastok':0,'lastopen':0,'send':2,'processerrors':0},utilsunit.getreportlastrun()) #check report
    #change channel type to file and do automaticretrycommunication: OK
    change_communication_type('unitretry_automatic_out','file')
    subprocess.call([pythoninterpreter,'bots-engine.py','--automaticretrycommunication'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':0,'lastreceived':2,'lasterror':0,'lastdone':2,'lastok':0,'lastopen':0,'send':2,'processerrors':0},utilsunit.getreportlastrun()) #check report
    
    #############route unitretry_mime: logic for mime-handling is different for resend
    change_communication_type('unitretry_mime_out','mimefile')
    subprocess.call([pythoninterpreter,'bots-engine.py','unitretry_mime'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':0,'lastreceived':1,'lasterror':0,'lastdone':1,'lastok':0,'lastopen':0,'send':1,'processerrors':0},utilsunit.getreportlastrun()) #check report
    #change channel type to ftp: errors (run twice)
    change_communication_type('unitretry_mime_out','ftp')
    subprocess.call([pythoninterpreter,'bots-engine.py','unitretry_mime'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':1,'lastreceived':1,'lasterror':1,'lastdone':0,'lastok':0,'lastopen':0,'send':0,'processerrors':1},utilsunit.getreportlastrun()) #check report
    change_communication_type('unitretry_mime_out','ftp')
    subprocess.call([pythoninterpreter,'bots-engine.py','unitretry_mime'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':1,'lastreceived':1,'lasterror':1,'lastdone':0,'lastok':0,'lastopen':0,'send':0,'processerrors':1},utilsunit.getreportlastrun()) #check report
    #change channel type to mimefile and do automaticretrycommunication: OK
    change_communication_type('unitretry_mime_out','mimefile')
    subprocess.call([pythoninterpreter,'bots-engine.py','--automaticretrycommunication'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':0,'lastreceived':2,'lasterror':0,'lastdone':2,'lastok':0,'lastopen':0,'send':2,'processerrors':0},utilsunit.getreportlastrun()) #check report



    #run automaticretrycommunication again: no new run is made, same results as last run 
    subprocess.call([pythoninterpreter,'bots-engine.py','--automaticretrycommunication'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':0,'lastreceived':2,'lasterror':0,'lastdone':2,'lastok':0,'lastopen':0,'send':2,'processerrors':0},utilsunit.getreportlastrun()) #check report
    #channel has type file: this goes OK
    change_communication_type('unitretry_mime_out','mimefile')
    subprocess.call([pythoninterpreter,'bots-engine.py','unitretry_mime'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':0,'lastreceived':1,'lasterror':0,'lastdone':1,'lastok':0,'lastopen':0,'send':1,'processerrors':0},utilsunit.getreportlastrun()) #check report
    
    #***run with communciation errors, run OK, communciation errors, run OK, run automaticretry
    #change channel type to ftp: errors
    change_communication_type('unitretry_mime_out','ftp')
    subprocess.call([pythoninterpreter,'bots-engine.py','unitretry_mime'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':1,'lastreceived':1,'lasterror':1,'lastdone':0,'lastok':0,'lastopen':0,'send':0,'processerrors':1},utilsunit.getreportlastrun()) #check report
    #channel has type file: this goes OK
    change_communication_type('unitretry_mime_out','mimefile')
    subprocess.call([pythoninterpreter,'bots-engine.py','unitretry_mime'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':0,'lastreceived':1,'lasterror':0,'lastdone':1,'lastok':0,'lastopen':0,'send':1,'processerrors':0},utilsunit.getreportlastrun()) #check report
    #change channel type to ftp: errors
    change_communication_type('unitretry_mime_out','ftp')
    subprocess.call([pythoninterpreter,'bots-engine.py','unitretry_mime'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':1,'lastreceived':1,'lasterror':1,'lastdone':0,'lastok':0,'lastopen':0,'send':0,'processerrors':1},utilsunit.getreportlastrun()) #check report
    #channel has type file: this goes OK
    change_communication_type('unitretry_mime_out','mimefile')
    subprocess.call([pythoninterpreter,'bots-engine.py','unitretry_mime'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':0,'lastreceived':1,'lasterror':0,'lastdone':1,'lastok':0,'lastopen':0,'send':1,'processerrors':0},utilsunit.getreportlastrun()) #check report
    #change channel type to file and do automaticretrycommunication: OK
    change_communication_type('unitretry_mime_out','mimefile')
    subprocess.call([pythoninterpreter,'bots-engine.py','--automaticretrycommunication'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':0,'lastreceived':2,'lasterror':0,'lastdone':2,'lastok':0,'lastopen':0,'send':2,'processerrors':0},utilsunit.getreportlastrun()) #check report
    #change channel type to ftp: errors
    change_communication_type('unitretry_mime_out','ftp')
    subprocess.call([pythoninterpreter,'bots-engine.py','unitretry_mime'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':1,'lastreceived':1,'lasterror':1,'lastdone':0,'lastok':0,'lastopen':0,'send':0,'processerrors':1},utilsunit.getreportlastrun()) #check report
    #channel has type file: this goes OK
    change_communication_type('unitretry_mime_out','mimefile')
    subprocess.call([pythoninterpreter,'bots-engine.py','unitretry_mime'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':0,'lastreceived':1,'lasterror':0,'lastdone':1,'lastok':0,'lastopen':0,'send':1,'processerrors':0},utilsunit.getreportlastrun()) #check report
    #change channel type to file and do automaticretrycommunication: OK
    change_communication_type('unitretry_mime_out','mimefile')
    subprocess.call([pythoninterpreter,'bots-engine.py','--automaticretrycommunication'])     #run bots
    botsglobal.db.commit()
    utilsunit.comparedicts({'status':0,'lastreceived':1,'lasterror':0,'lastdone':1,'lastok':0,'lastopen':0,'send':1,'processerrors':0},utilsunit.getreportlastrun()) #check report


    logging.shutdown()
    botsglobal.db.close
    print 'Tests OK!!!' 

